package com.starwish.starwishblog.web.back;

import com.github.pagehelper.PageInfo;
import com.starwish.starwishblog.entity.Role;
import com.starwish.starwishblog.result.ResultResponse;
import com.starwish.starwishblog.service.RoleService;
import com.starwish.starwishblog.vo.RoleVo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("admin/role")
@Api(value = "操作角色的接口")
public class RoleController {

    @Autowired
    RoleService roleService;

    @GetMapping("/rlist")
    public ResultResponse<PageInfo<Role>> rlist(@RequestParam(name="now",defaultValue = "1")Integer now,
                                                @RequestParam(name="size",defaultValue = "5")Integer size,
                                                String name){
        ResultResponse<PageInfo<Role>> result = null;
        try {
            PageInfo<Role> pageInfo = roleService.queryByCons(now,size,name);
            result = new ResultResponse<>(200,"查询角色成功",pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201,"失败");
        }
        return result;
    }

    @GetMapping("/list")
    public ResultResponse<PageInfo<RoleVo>> list(@RequestParam(name="now",defaultValue = "1")Integer now,
                                                 @RequestParam(name="size",defaultValue = "5")Integer size,
                                                 String name){
        ResultResponse<PageInfo<RoleVo>> result = null;
        try {
            PageInfo<RoleVo> pageInfo = roleService.queryRoleVoByCons(now,size,name);
            result = new ResultResponse<>(200,"查询角色成功",pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201,"失败");
        }
        return result;
    }

    @GetMapping("/getAll")
    public ResultResponse<List<Role>> list(){
        ResultResponse<List<Role>> result = null;
        try {
            List<Role> roleList = roleService.queryAllRoles();
            result = new ResultResponse<>(200,"查询角色列表成功",roleList);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询角色列表失败");
        }
        return result;
    }

    @DeleteMapping("/del/{id}")
    public ResultResponse<Void> delById(@PathVariable(name="id")Integer id){
        ResultResponse<Void> result = null;
        try {
            int i = roleService.removeByKey(id);
            if(i > 0){
                result = new ResultResponse<>(200,"删除成功");
            }else{
                result = new ResultResponse<>(201,"删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(205,"系统错误");
        }
        return result;
    }

    @PutMapping("/add")
    public ResultResponse<Void> add(@RequestBody Role role){
        ResultResponse<Void> result = null;
        try {
            int i = roleService.add(role);
            if(i > 0){
                result = new ResultResponse<>(200,"添加成功");
            }else{
                result = new ResultResponse<>(201,"添加失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(205,"系统错误");
        }
        return result;
    }

    @PutMapping("/modify")
    public ResultResponse<Void> modify(@RequestBody Role role){
        ResultResponse<Void> result = null;
        try {
            Role myRole = roleService.queryRoleById(Integer.parseInt(role.getId().toString()));
            myRole.setName(role.getName());
            int i = roleService.modify(myRole);
            if(i > 0){
                result = new ResultResponse<>(200,"修改成功");
            }else{
                result = new ResultResponse<>(201,"修改失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(205,"系统错误");
        }
        return result;
    }


    @GetMapping("/getCheckedPer/{id}")
    public ResultResponse<List<Integer>> getCheckedPer(@PathVariable("id") Integer id){
        ResultResponse<List<Integer>> result = null;

        try {
            List<Integer> longs = roleService.queryPerIdsoByRoleKey(id);
            result = new ResultResponse<>(200,"查询成功!",longs);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询失败!");
        }
        return result ;
    }


    @PostMapping("/authorityToRole")
    public ResultResponse<Void> authorityToRole(@RequestBody RoleVo roleVo){
        ResultResponse<Void> result = null;

        try {
            //授权
            roleService.authorityToRole(roleVo);
            result = new ResultResponse<>(200,"角色授权成功!");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"角色授权失败!");
        }
        return result ;
    }


}
